package com.ddone.datax.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

/**
 * @author ddone
 * @date 2024/5/24-18:49
 */
public class LocalFileReaderUtil {
    /**
     * 获取文件内容
     *
     * @param path 本地文件路径
     * @return 返回字符串
     */
    public static String getFileContent(String path) {
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(path))) {
            return reader.lines().collect(Collectors.joining(System.lineSeparator()));
        } catch (IOException e) {
            throw new RuntimeException("Error reading file: " + e.getMessage(), e);
        }
    }

    public static String getResourceFileContent(String path) {
        ClassLoader classLoader = LocalFileReaderUtil.class.getClassLoader();
        URL resource = classLoader.getResource(path);
        if (resource == null) {
            throw new RuntimeException("Error reading file: 文件路径不存在");
        }
        return getFileContent(resource.getPath());
    }

    public static void main(String[] args) {
       String file = "mysql2hdfs.json";
       System.out.println(getResourceFileContent(file));
    }
}
